package Hander.Updata;

import View.LeaderView;
import View.Updata.UpdataPersonnelView;
import entity.Personnel;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdataPersonnelViewHander implements ActionListener {
    private UpdataPersonnelView updataPersonnelView;
    private LeaderView leaderView;
    public UpdataPersonnelViewHander(UpdataPersonnelView updataPersonnelView, LeaderView leaderView) {
        this.updataPersonnelView = updataPersonnelView;
        this.leaderView=leaderView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("修改".equals(text)){
            PersonnelService personnelService=new PersonnelServiceImpl();
            Personnel personnel=updataPersonnelView.buildAchievement();

            int key=Integer.parseInt(leaderView.getSelectedStudentIds()[0]);
            Boolean addresult=personnelService.updata(key,personnel.getAllattributes(),updataPersonnelView.getTablesName());
            if(addresult){
                leaderView.reloadTable(updataPersonnelView.getTablesName());
                JOptionPane.showMessageDialog(updataPersonnelView,"修改成功");
                updataPersonnelView.dispose();
            } else{
                System.out.println("修改错误！");
                JOptionPane.showMessageDialog(updataPersonnelView,"修改失败");
            }
        }
    }
}
